package main

import (
	"fmt"
)

//Slice

func updateSlice(arr []int) {
	arr[0] = 100
}

func main() {
	arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
	s1 := arr[2:6]
	fmt.Println("s1 = ", s1)
	updateSlice(s1)
	fmt.Println("updateSlice(s1) After")
	fmt.Println("s1 = ", s1)
	fmt.Println("arr = ", arr)

	fmt.Println("arr[2:] = ", arr[2:])
	fmt.Println("arr[:6] = ", arr[:6])
	fmt.Println("arr[:] = ", arr[:])

	s2 := arr[:]
	//ReSlice
	fmt.Println("ReSlice")
	fmt.Println(s2)
	s2 = s2[:5]
	fmt.Println(s2)
	s2 = s2[2:]
	fmt.Println(s2)

	//ReSlice进阶
	arr1 := [...]int{0, 1, 2, 3, 4, 5, 6, 7}
	t1 := arr1[2:6]
	t2 := t1[3:5]

	fmt.Println("t1 = ", t1)
	fmt.Printf("t1=%v, len(t1)=%d, cap(t1)=%d\n",
		t1, len(t1), cap(t1))
	fmt.Println("t2 = ", t2) //Slice可以向后扩展

	//向Slice添加元素
	//添加元素超过cap，系统会重新分配更大的底层数组
	//
	t3 := append(t2, 10)
	t4 := append(t3, 11)
	t5 := append(t4, 12)
	fmt.Println("t3 = ", t3)
	fmt.Println("t4 = ", t4)
	fmt.Println("t5 = ", t5)
	fmt.Println("arr1 =", arr1, "len", len(arr1))
	fmt.Println(&t3 == &t2)
}
